﻿#include "Player.h"

//***************************************************************
//构造与析构
Player::Player()
{
    playerScience = new Development(represent);
}

Player::Player(int represent)
{
    this->represent = represent;
    playerScience = new Development(represent);
}

Player::~Player()
{
    delete playerScience;
    //清空池子
    list<Human*>::iterator iter_deleHuman = human.begin();
    list<Building*>::iterator iter_deleBuild = build.begin();

    while(iter_deleHuman!=human.end())iter_deleHuman = deleteHuman(iter_deleHuman);
    while(iter_deleBuild!=build.end())iter_deleBuild = deleteBuilding(iter_deleBuild);
}

//***************************************************************
//添加实例对象
Building* Player::addBuilding(int Num, int BlockDR, int BlockUR , double percent)
{
    Building *newbuilding = NULL;
    if(Num == BUILDING_FARM) newbuilding = new Building_Resource(Num,BlockDR,BlockUR,getCiv() , playerScience , represent , percent);
    else newbuilding=new Building(Num,BlockDR,BlockUR, getCiv(), playerScience , represent , percent);

    build.push_back(newbuilding);
    return newbuilding;
}

int Player::addHuman(int Num, double DR, double UR)
{
    Human *newhuman=new Human(Num,DR,UR , playerScience , represent);

    call_debugText("blue"," 产生了新的"+newhuman->getChineseName()+"(编号:" + QString::number(newhuman->getglobalNum()) + ")",represent);

    human.push_back(newhuman);
    humanNumIncrease(newhuman);
    return 0;
}

Army* Player::addArmy(int Num , double DR , double UR)
{
    Army *newArmy = new Army(DR , UR, Num ,  playerScience , represent);
    call_debugText("blue"," 产生了新的"+newArmy->getChineseName()+"(编号:" + QString::number(newArmy->getglobalNum()) + ")",represent);

    if(g_frame > 10 && represent == NOWPLAYERREPRESENT)
        soundQueue.push("Army_Born");

    human.push_back(newArmy);
    humanNumIncrease(newArmy);
    return newArmy;
}

int Player::addFarmer(double DR, double UR)
{
    Farmer *newfarmer=new Farmer(DR,UR , playerScience , represent);
    call_debugText("blue"," 产生了新的村民(编号:" + QString::number(newfarmer->getglobalNum()) + ")",represent);

    if(g_frame > 10 && represent == NOWPLAYERREPRESENT)
        soundQueue.push("Villager_Born");

    human.push_back(newfarmer);
    humanNumIncrease(newfarmer);
    return 0;
}


//删除实例对象
list<Human*>::iterator Player::deleteHuman( list<Human*>::iterator iterDele )
{
    delete *iterDele;
    return human.erase(iterDele);
}

list<Building*>::iterator Player::deleteBuilding( list<Building*>::iterator iterDele )
{
    //删除的是HOME
    if((*iterDele)->getNum() == BUILDING_HOME)
    {
        if((*iterDele)->isConstructed())
            playerScience->subHome();
    }
    //删除的是市镇中心
    else if((*iterDele)->getNum() == BUILDING_CENTER) playerScience->subCenter();
    //删除的是其余有时代特征的建筑
    else if((*iterDele)->getNum() != BUILDING_FARM) playerScience->sub_civiBuildNum((*iterDele)->getNum());

    delete *iterDele;
    return build.erase(iterDele);
}





